package org.demo.flypig.orm;

import org.h2.tools.Server;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

	private static Server server;
	private static SessionFactory sessionFactory;
	
	static {
		try {
			server = Server.createWebServer("").start();
			sessionFactory = new Configuration().configure().buildSessionFactory();
		} catch (Throwable ex) {
			ex.printStackTrace();
			throw new ExceptionInInitializerError(ex);
		}
	}
	
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public static void shutdown() {
		if (getSessionFactory() != null && !getSessionFactory().isClosed()) {
			getSessionFactory().close();
		}
		if (server != null && server.isRunning(true)) {
			server.stop();
		}
	}
}
